package cassandrawindow;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public class HandleLogBannerDomain {

    public void handleData()
    {
        MysqlDataManagerMain mysqlMain = new MysqlDataManagerMain();
        MysqlDataManager mysql = new MysqlDataManager();
        //String currentDate = TimeUtil.getCurrentTimeInString();//2012-07-24
        String currentDate = HandleDataTableDomainDate.handleDate();//2012-07-24
        //String currentDate = CONSTANT.Date;//2012-07-24
        //get all banner running
        Map<String,String> allBannerRunning=new HashMap<>();        
        allBannerRunning = mysqlMain.getAllBannerRunning();
        //get all bannner trong log
        Map<String,String> allBannerLog=new HashMap<>();
        allBannerLog = mysql.getAllBannerRunning(currentDate);
        //tinh click view trong log        
        Iterator iterator = allBannerRunning.entrySet().iterator();

        StringBuilder sbstr  = new StringBuilder();
	while (iterator.hasNext()) {
                /*
                 * Lay du lieu banneri id mà user chọn và những site mà banner đó đặt vào
                 */
		Map.Entry mapEntry = (Map.Entry) iterator.next();
                String bannerid     = (String) mapEntry.getKey();
                //String bannerid     = "180092";
                String list_site = (String) mapEntry.getValue();
                
                if(allBannerLog.containsKey(bannerid))
                {
                    //String test = allBannerLog.get("180092");
                    //lay ra cac domain
                    String[] arrValue = new String[200];
                    arrValue = list_site.split(",");
                    StringBuilder sb  = new StringBuilder();
                    sb.append("'");
                    for (int i = 0 ;i < arrValue.length;i++)
                    {
                        if(i == (arrValue.length -1)){
                            sb.append(arrValue[i]).append("'");
                        } else{
                            sb.append(arrValue[i]).append("'").append(",").append("'");
                        }
                    }
                    try {
                        /*
                        * Lay du lieu click va view doi voi bannerid trong log
                        */
                        String value = allBannerLog.get(bannerid);
                        String domain = value.split("_")[0];
                        String clickLog = value.split("_")[1];
                        String viewLog = value.split("_")[2];
                        if(Integer.valueOf(clickLog) > 0)
                        {
                            /*
                             * Lay tong view va tong click doi voi bannerid ung vơi cac domain ma dat
                             */
                            String valueHang    = mysql.getDetailBannerByDomain(bannerid, currentDate, sb.toString());
                            String clickHang    = valueHang.split("_")[0];
                            String viewHang     = valueHang.split("_")[1];

                            String strResult = "(" + bannerid + ",'" + list_site + "','" + domain + "'," +clickHang +"," + clickLog + "," + viewHang +"," + viewLog +",'" + currentDate +"')";
                            sbstr.append(strResult).append(",");

                            if(sbstr.toString().length() > 20000)
                            {
                                //xu li cho du lieu cuoi
                                String relStr = sbstr.toString();
                                String valInsert = relStr.substring(0, (relStr.length()-1));
                                mysql.insertDataDomainDataBannerLog(valInsert);
                                sbstr.setLength(0);
                            }
                        }
                    } catch (Exception ex){
                        System.out.println("error :"+ex.getMessage());
                    }
                }
                
	}

        if(sbstr.toString().length() > 0)
        {
            // MysqlDataManager mysql = new MysqlDataManager();
            //xu li cho du lieu cuoi
            String relStr = sbstr.toString();
            String valInsert = relStr.substring(0, (relStr.length()-1));
            mysql.insertDataDomainDataBannerLog(valInsert);
            sbstr.setLength(0);
        }
        
        //tinh click view voi nhung domain ma khach hang treo



    }    
}
